package cn.livingCloud.entity.core;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.entity.system.Setting;
import cn.livingCloud.entity.user.Member;
import cn.livingCloud.repository.PushGroupMemberMapper;
import cn.livingCloud.repository.ReportRuleMapper;
import cn.livingCloud.repository.SettingMapper;
import cn.livingCloud.service.TemplateInfo;
import cn.livingCloud.service.WxTemplateMsgService;
import cn.livingCloud.service.impl.QuartzJobServiceImpl;
import cn.livingCloud.utils.UuidGenerator;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.List;

/**
 * 推送组
 */
public class PushGroup extends BaseEntity implements Job, JobInfo {
    private static final String JOB_NAME_TEMPLATE = "%s[%s]";
    private static final String JOB_GROUP = "PushGroup";

    private transient ReportRuleMapper reportRuleMapper = BeanProvider.getBean(ReportRuleMapper.class);
    private transient PushGroupMemberMapper groupMemberMapper = BeanProvider.getBean(PushGroupMemberMapper.class);
    private transient SettingMapper settingMapper = BeanProvider.getBean(SettingMapper.class);
    private transient WxTemplateMsgService wxTemplateMsgService = BeanProvider.getBean(WxTemplateMsgService.class);

    private String uuid = UuidGenerator.generate();
    //状态 默认是启用
    private boolean state = false;
    //推送组名称
    private String name;
    //报表规则
    private String reportRuleUuid;

    /**
     * 推送 定时任务表达式
     */
    private String cronExpression;

    public PushGroup() {
    }

    public String getUuid() {
        return uuid;
    }

    public PushGroup setUuid(String uuid) {
        this.uuid = uuid;
        return this;
    }

    public String getName() {
        return name;
    }

    public PushGroup setName(String name) {
        this.name = name;
        return this;
    }

    @Override
    public String getJobName() {
        return String.format(JOB_NAME_TEMPLATE, this.getName(), this.uuid);
    }

    @Override
    public String getJobGroup() {
        return JOB_GROUP;
    }

    public String getCronExpression() {
        return cronExpression;
    }

    public PushGroup setCronExpression(String cronExpression) {
        this.cronExpression = cronExpression;
        return this;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        PushGroup pushGroup = (PushGroup) jobExecutionContext.get(QuartzJobServiceImpl.DATA_NAME);
        Setting setting = settingMapper.findSetting();
        List<Member> memberList = groupMemberMapper.getMemberByGroup(pushGroup.getUuid());
        for (Member member : memberList) {
            TemplateInfo templateInfo = buildInfo(member, pushGroup, setting);
            wxTemplateMsgService.sendTemplate(templateInfo);
        }
    }

    private TemplateInfo buildInfo(final Member member, PushGroup pushGroup, final Setting setting) {
        //TODO:添加报表模板消息
        return new TemplateInfo() {
            @Override
            public String getTemplateId() {
                return null;
            }

            @Override
            public String getOpenid() {
                return member.getOpenid();
            }

            @Override
            public String getUrl() {
//                pushGroup.getUrl()
                return null;
            }

            @Override
            public Object getData() {
//                pushGroup.getData()
                return null;
            }

            @Override
            public String getTemplateContent() {
                return null;
            }
        };
    }

    @Override
    public String toString() {
        return "PushGroup{" +
                "cronExpression='" + cronExpression + '\'' +
                ", name='" + name + '\'' +
                ", uuid='" + uuid + '\'' +
                '}';
    }

    public String getReportRuleUuid() {
        return reportRuleUuid;
    }

    public void setReportRuleUuid(String reportRuleUuid) {
        this.reportRuleUuid = reportRuleUuid;
    }

    public ReportRule getReportRule() {

        return reportRuleMapper.selectByPrimaryKey(this.reportRuleUuid);
    }

    public boolean isState() {
        return state;
    }

    public void setState(boolean state) {
        this.state = state;
    }
}